import pygame
from .. import setup
from .. import tools
from .. import constants as C
from ..components import info


# 主菜单类
class MainMenu:
    def __init__(self):
        # 游戏关键数据字典(分数、硬币数、生命数、角色状态等):
        game_info = {
            'score': 0,
            'coin': 0,
            'lives': 3,
            'player_state': 'small'
        }
        self.start(game_info)

    # 将初始化方法放入start方法内，使其能够被反复调用，方便不同阶段信息共享
    # 参数game_info为各个阶段传递的关键游戏信息(时间、生命数等)
    # 每次只需更新game_info并调用start方法即可实现数据更新和传递
    def start(self, game_info):
        self.game_info = game_info
        self.setup_background()  # 设置地图背景
        self.setup_player()  # 设置玩家
        self.setup_cursor()  # 设置光标
        self.info = info.Info('main_menu', self.game_info)  # 实例化Info类
        self.finished = False  # 初始化完结标志finished状态为False
        self.next = 'load_screen'  # 下一阶段调用的游戏载入类名

    def setup_background(self):
        self.background = setup.GRAPHICS['level_1']  # 背景图片设置
        self.background_rect = self.background.get_rect()  # 背景图片矩形框创建
        # 游戏主界面背景参数配置
        self.background = pygame.transform.scale(self.background,
                                                 (int(self.background_rect.width * C.BG_MULTI),
                                                  int(self.background_rect.height * C.BG_MULTI)))
        self.viewport = setup.SCREEN.get_rect()  # 游戏画面滚动框大小设置
        # 游戏主界面图标参数配置
        self.caption = tools.get_image(setup.GRAPHICS['title_screen'],
                                       1, 60, 176, 88, (255, 0, 220), C.BG_MULTI)

    def setup_player(self):
        # 游戏主界面角色配置
        self.player_image = tools.get_image(setup.GRAPHICS['mario_bros'],
                                            178, 32, 12, 16, (0, 0, 0), C.PLAYER_MULTI)

    def setup_cursor(self):
        # 游戏主界面光标配置
        # 将cursor扩展为pygame.sprite.Sprite()/精灵类
        self.cursor = pygame.sprite.Sprite()
        # 获取光标图片
        self.cursor.image = tools.get_image(setup.GRAPHICS['item_objects'],
                                            24, 160, 8, 8, (0, 0, 0), C.CURSOR_MULTI)
        # 定义矩形变量rect为光标图片获取矩形,并定义其x,y坐标值
        rect = self.cursor.image.get_rect()
        rect.x, rect.y = (220, 360)
        # 将rect赋值给光标的矩形参数
        self.cursor.rect = rect
        # 初始化光标状态机为1P(对应主界面1 PLAYER选项)
        self.cursor.state = '1P'

    # 光标更新方法,用以响应按键keys更新光标状态
    def update_cursor(self, keys):
        # 如果按键为K_UP则设置光标状态为1P,光标y坐标为360
        if keys[pygame.K_UP]:
            self.cursor.state = '1P'
            self.cursor.rect.y = 360
        # 否则如果按键为K_DOWN则设置光标状态为2P,光标y坐标为405
        elif keys[pygame.K_DOWN]:
            self.cursor.state = '2P'
            self.cursor.rect.y = 405
        # 按键为回车则判断当前光标所在状态进入对应模式
        elif keys[pygame.K_RETURN]:
            self.reset_game_info()  # 重置游戏关键数据信息(防止死亡后生命数继续变为负数)
            if self.cursor.state == '1P':
                self.finished = True
            elif self.cursor.state == '2P':
                self.finished = True

    # 重置游戏关键数据信息(防止死亡后生命数继续变为负数)
    def reset_game_info(self):
        self.game_info.update({
            'score': 0,
            'coin': 0,
            'lives': 3,
            'player_state': 'small'
        })

    def update(self, surface, keys):  # 主界面更新
        # 更新画面前画面组件状态的更新
        self.update_cursor(keys)

        # 利用blit()方法绘制更新后状态下的主界面
        surface.blit(self.background, self.viewport)
        surface.blit(self.caption, (170, 100))
        surface.blit(self.player_image, (110, 490))
        surface.blit(self.cursor.image, self.cursor.rect)

        self.info.update()
        self.info.draw(surface)
